//
//  PersonalInfoVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/9.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class PersonalInfoVC: BaseViewController {
    
    var dataModel : PersonalDataModel?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "个人信息"

        plainTableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0)
        
        plainTableView.tableFooterView = UIView()
        
    
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: PersonalInfoTopCell.self), bundle: nil), forCellReuseIdentifier: PersonalInfoTopCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: PersonalInfoMiddleCell.self), bundle: nil), forCellReuseIdentifier: PersonalInfoMiddleCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: PersonalInfoBottomCell.self), bundle: nil), forCellReuseIdentifier: PersonalInfoBottomCell.identifer())
        
        
                weak var weakSelf = self
        
                plainTableView.mj_header =  ZMRefreshHeader.init {
        
                    weakSelf!.loadData()
        
                }
        
        
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        plainTableView.mj_header.beginRefreshing()

    }


    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    



}


extension PersonalInfoVC{


    func loadData(){
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_index&a=index", parameters: nil) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                
                let temp = responseObject?["datas"]?["member_info"] as! [String : AnyObject]
                
                self!.dataModel = PersonalDataModel(dic: temp)
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
        
        
    }




}





// MARK:- tableview代理方法
extension PersonalInfoVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil{
            
            return 0
            
        }
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModel == nil{
            
            return 0
            
        }
        
        return 3
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        switch indexPath.row {
        case 0:
            let tempCell : PersonalInfoTopCell =  tableView.dequeueReusableCell(withIdentifier: PersonalInfoTopCell.identifer()) as! PersonalInfoTopCell
            tempCell.iconUrl = dataModel?.avator
            tableCell = tempCell
        case 2:
            let tempCell : PersonalInfoMiddleCell =  tableView.dequeueReusableCell(withIdentifier: PersonalInfoMiddleCell.identifer()) as! PersonalInfoMiddleCell
            
            tempCell.userName = dataModel?.user_name
            
            tableCell = tempCell
            
        default:
            
            let tempCell : PersonalInfoBottomCell =  tableView.dequeueReusableCell(withIdentifier: PersonalInfoBottomCell.identifer()) as! PersonalInfoBottomCell
            
            tempCell.nikName = dataModel?.nickname
            
            
            tableCell = tempCell
            
            
        }
        
        
        return tableCell!
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.row {
        case 0:
            return 90
            
        default:
            return 46
            
        }
        
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.row == 0 {
            
            let temp = UpLoadIconController()
            
            temp.urlStr = dataModel!.upload_avatar_url
            
            navigationController?.pushViewController(temp, animated: true)
            
            
        }else if indexPath.row == 1{
            
            let temp = ModifyNikNameVC()
            
            temp.userName = dataModel?.nickname
            
            navigationController?.pushViewController(temp, animated: true)
            
            
        }else{
            
            alert(str: "用户名不支持修改哟!")
            
        }
        
        
    }
    
    
    
    func alert(str : String) {
        
        let tempVC = UIAlertController(title: nil, message: str, preferredStyle: .alert)
        
        let action1 = UIAlertAction(title: "确定", style: .default) { (action) in
            
        }
        
        
        tempVC.addAction(action1)
        
        present(tempVC, animated: true, completion: nil)
        
        
    }
    
    
    
}

